<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Adminlog_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_adminlog';
	}
        
        public function statitic($params1,$params2){
            $this->db->select('count(*) AS total');
            $this->db->from($this->table_name);
            $this->db->like('lastLogin',$params1);
            $this->db->like('logAction',$params2);
            $query = $this->db->get();
//            echo $this->db->last_query();exit;
            return $query->result(); 
        }
        
        public function statiticUsers(){
            $this->db->select('usr.username, count(log.userid) AS total');
            $this->db->from($this->table_name .' AS log');
            $this->db->join('users AS usr','usr.id = log.userid');
            $this->db->group_by('log.userid');
            $query = $this->db->get();
//            echo $this->db->last_query();exit;
            return $query->result(); 
        }


        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'int',
						)
				),
				array(
						'name' => 'username',
						'label' => $this->lang->line('username'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'text',
						)
				),array(
						'name' 	=> 'logAction',
						'label' => $this->lang->line('status'),
						'width' => '30%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'text',
						)
				),
				array(
						'name' 	=> 'ipaddress',
						'label' => $this->lang->line('ipaddress'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'int',
						)
				),
                                array(
						'name' 	=> 'lastLogin',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => 'DESC',
						'searchoptions' => array(
								'type' 	=> 'text',
						)
				),
                                array(
						'name' 	=> 'agent_code',
						'label' => $this->lang->line('agent_code'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => array(
								'type' 	=> 'text',
						)
				)
		);
	}
	
	public function json_data($controller, $right){
		$this->datatables
		->select("users.username,book_adminlog.id,book_adminlog.logAction,book_adminlog.ip,book_adminlog.lastLogin,book_agent_code.pcc")
		->from('book_adminlog')
                ->join('book_agent_code','book_adminlog.agent_code = book_agent_code.id')
                ->join('users','users.id = book_adminlog.userid');
		//->unset_column('id');
		
		if($this->session->userdata['group_id'] == 79){
			$this->datatables->where('logAction like "[Delete%"');
		}
	
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();
                $count = 1;
                
		foreach($datatables['aaData'] as $item){
			$ouput['aaData'][] = array(
					$count++,
					$item['username'],
					$item['logAction'],
                                        $item['ip'],
                                        date("d-m-Y H:i:s",strtotime($item['lastLogin'])),
                                        $item['pcc'],
					$this->add_button($controller, $right, $item),
			);
		}
	
		return json_encode($ouput);
	}

}